<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\admin\controller;
use think\Db;
use org\Qiniu;

/**
 * Class 统一订单类
 * @package app\admin\controller
 */
class Order extends Base
{
    public function viporder(){
        if(request()->isAjax()) {
            $map = array(
                'acid' => 1,
                'goodstype'=>'vip'
            );
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('mediaorder')
                ->where($map)
                ->page($Nowpage, $limits)
                ->order($order)
                ->order('ordertime desc')
                ->select();
            $total=Db::name('mediaorder')
                ->where($map)
                ->count();

            foreach ($data as $k=>$v){
                $userinfo=db('user')->where(['acid'=>1,'id'=>$v['uid']])->find();
                $goodsinfo=db('viptime')->where(['acid'=>1,'id'=>$v['menuid']])->find();
                $data[$k]['goodsname']=$goodsinfo['name'];
                $data[$k]['avatar']=$userinfo['avatar'];
                $data[$k]['nickname']=$userinfo['nickname'];
                if(session('agid')!='1' && session('username')!='admin') {
                    if ($userinfo['avatar'] == '') {
                        $data[$k]['nickname'] = substr_replace($userinfo['nickname'], '******', 8, 6);
                    }
                }
                $data[$k]['ordertime']=date('Y-m-d H:i:s',$v['ordertime']);
                $data[$k]['paytime']=$v['paytime']?date('Y-m-d H:i:s',$v['paytime']):'';
                $data[$k]['is_pay']=$v['is_pay']==1?'已支付':'未支付';
                if($v['pay_type']=='wxpay'){
                    $data[$k]['pay_type']='微信支付';
                }elseif ($v['pay_type']=='alipay'){
                    $data[$k]['pay_type']='支付宝支付';
                }elseif ($v['pay_type']=='credit'){
                    $data[$k]['pay_type']='积分兑换';
                }elseif ($v['pay_type']=='yhm'){
                    $data[$k]['pay_type']='优惠码兑换';
                }elseif ($v['pay_type']=='balance'){
                    $data[$k]['pay_type']='余额支付';
                }
                if($v['apptype']=='MP-WEIXIN'){
                    $data[$k]['apptype']='微信小程序';
                }elseif ($v['apptype']=='MP-TOUTIAO'){
                    $data[$k]['apptype']='抖音|头条';
                }elseif ($v['apptype']=='MP-BAIDU'){
                    $data[$k]['apptype']='百度小程序';
                }elseif ($v['apptype']=='MP-ALIPAY'){
                    $data[$k]['apptype']='支付宝小程序';
                }elseif ($v['apptype']=='APP-PLUS'){
                    $data[$k]['apptype']='APP';
                }
                $data[$k]['avatar']=to_media_url($data[$k]['avatar']);
            }

            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function course(){
        if(request()->isAjax()) {
            $map = array(
                'acid' => 1,
                'goodstype'=>'course'
            );
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            if(session('agid')=='6'){
                $map1 = array(
                    'a.acid' => 1,
                    'a.goodstype'=>'course'
                );
                $admininfo=db('teacher')->where('uid',session('uid'))->find();
                if(!empty($admininfo)){
                    $map1['b.tid']=$admininfo['id'];
                }
                $data = Db::name('mediaorder')
                    ->alias ('a')
                    ->join('coursemenu b', 'a.menuid = b.id','left')
                    ->field('a.*,b.menuname as goodsname')
                    ->where($map1)
                    ->page($Nowpage, $limits)
                    ->order($order)
                    ->order('a.ordertime desc')
                    ->select();
                $total=Db::name('mediaorder')
                    ->alias ('a')
                    ->join('coursemenu b', 'a.menuid = b.id','left')
                    ->field('a.*,b.menuname as goodsname')
                    ->where($map1)
                    ->count();
            }else{
                $data = Db::name('mediaorder')
                    ->where($map)
                    ->page($Nowpage, $limits)
                    ->order($order)
                    ->order('ordertime desc')
                    ->select();
                $total=Db::name('mediaorder')
                    ->where($map)
                    ->count();
            }
            foreach ($data as $k=>$v){
                $userinfo=db('user')->where(['acid'=>1,'id'=>$v['uid']])->find();
                $goodsinfo=db('coursemenu')->where(['acid'=>1,'id'=>$v['menuid']])->find();
                $data[$k]['goodsname']=$goodsinfo['menuname'];
                $data[$k]['avatar']=$userinfo['avatar'];
                $data[$k]['nickname']=$userinfo['nickname'];
                if(session('agid')!='1' && session('username')!='admin') {
                    if ($userinfo['avatar'] == '') {
                        $data[$k]['nickname'] = substr_replace($userinfo['nickname'], '******', 8, 6);
                    }
                }
                $data[$k]['ordertime']=date('Y-m-d H:i:s',$v['ordertime']);
                $data[$k]['paytime']=$v['paytime']?date('Y-m-d H:i:s',$v['paytime']):'';
                $data[$k]['endtime_cn']=$v['endtime']?date('Y-m-d H:i:s',$v['endtime']):'不限';
                $data[$k]['is_pay']=$v['is_pay']==1?'已支付':'未支付';
                if($v['pay_type']=='wxpay'){
                    $data[$k]['pay_type']='微信支付';
                }elseif ($v['pay_type']=='alipay'){
                    $data[$k]['pay_type']='支付宝支付';
                }elseif ($v['pay_type']=='credit'){
                    $data[$k]['pay_type']='积分兑换';
                }elseif ($v['pay_type']=='yhm'){
                    $data[$k]['pay_type']='优惠码兑换';
                }elseif ($v['pay_type']=='balance'){
                    $data[$k]['pay_type']='余额支付';
                }
                if($v['apptype']=='MP-WEIXIN'){
                    $data[$k]['apptype']='微信小程序';
                }elseif ($v['apptype']=='MP-TOUTIAO'){
                    $data[$k]['apptype']='抖音|头条';
                }elseif ($v['apptype']=='MP-BAIDU'){
                    $data[$k]['apptype']='百度小程序';
                }elseif ($v['apptype']=='MP-ALIPAY'){
                    $data[$k]['apptype']='支付宝小程序';
                }elseif ($v['apptype']=='APP-PLUS'){
                    $data[$k]['apptype']='APP';
                }else{
                    $data[$k]['apptype']=$v['apptype'];
                }
                $data[$k]['avatar']=to_media_url($data[$k]['avatar']);
            }

            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function mall(){
//        if(session('agid')=='2') {
//            return $this->error('抱歉，您没有操作权限');
//        }
        if(request()->isAjax()) {
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('mediaorder')
                ->where(['acid'=>1,'goodstype'=>'mall'])
                ->page($Nowpage, $limits)
                ->order($order)
                ->order('ordertime desc')
                ->select();
            foreach ($data as $k=>$v){
                $userinfo=db('user')->where(['acid'=>1,'id'=>$v['uid']])->find();
                if(strstr($v['menuid'],',')){

                }else{
                    $goodsinfo=db('goods')->where(['acid'=>1,'id'=>$v['menuid']])->find();
                    $data[$k]['goodsname']=$goodsinfo['name'];
                }
                $shrinfo=db('user_address')->where(['acid'=>1,'uid'=>$v['uid'],'id'=>$v['addressid']])->find();
                if(session('agid')=='2') {
                    $shrinfo['phone'] = substr_replace($shrinfo['phone'], '*******', 3, 7);
                }
                $data[$k]['shrinfo']=$shrinfo['shr_name'].','.$shrinfo['phone'].','.$shrinfo['area'].$shrinfo['address'];
                $data[$k]['avatar']=$userinfo['avatar'];
                $data[$k]['nickname']=$userinfo['nickname'];
                if(session('agid')!='1' && session('username')!='admin') {
                    if ($userinfo['avatar'] == '') {
                        $data[$k]['nickname'] = substr_replace($userinfo['nickname'], '******', 8, 6);
                    }
                }
                $data[$k]['ordertime']=date('Y-m-d H:i:s',$v['ordertime']);
                $data[$k]['paytime']=$v['paytime']?date('Y-m-d H:i:s',$v['paytime']):'';
                $data[$k]['is_pay']=$v['is_pay']==1?'已支付':'未支付';
                if($v['pay_type']=='wxpay'){
                    $data[$k]['pay_type']='微信支付';
                }elseif ($v['pay_type']=='alipay'){
                    $data[$k]['pay_type']='支付宝支付';
                }elseif ($v['pay_type']=='credit'){
                    $data[$k]['pay_type']='积分兑换';
                }elseif ($v['pay_type']=='yhm'){
                    $data[$k]['pay_type']='优惠码兑换';
                }elseif ($v['pay_type']=='balance'){
                    $data[$k]['pay_type']='余额支付';
                }
                if($v['apptype']=='MP-WEIXIN'){
                    $data[$k]['apptype']='微信小程序';
                }elseif ($v['apptype']=='MP-TOUTIAO'){
                    $data[$k]['apptype']='抖音|头条';
                }elseif ($v['apptype']=='MP-BAIDU'){
                    $data[$k]['apptype']='百度小程序';
                }elseif ($v['apptype']=='MP-ALIPAY'){
                    $data[$k]['apptype']='支付宝小程序';
                }elseif ($v['apptype']=='APP-PLUS'){
                    $data[$k]['apptype']='APP';
                }
                if($v['fh_state']==0){
                    $data[$k]['fh_state']='未发货';
                }elseif($v['fh_state']==1){
                    $data[$k]['fh_state']='已发货';
                }elseif($v['fh_state']==2){
                    $data[$k]['fh_state']='已收货';
                }elseif($v['fh_state']==3){
                    $data[$k]['fh_state']='已完成';
                }elseif($v['fh_state']==4){
                    $data[$k]['fh_state']='退款';
                }
                $data[$k]['avatar']=to_media_url($data[$k]['avatar']);
            }
            $total=Db::name('mediaorder')
                ->where(['acid'=>1,'goodstype'=>'mall'])
                ->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function pxcourse(){
        if(request()->isAjax()) {
            $map = array(
                'acid' => 1,
                'goodstype'=>'pxcourse'
            );
            if(session('agid')=='7'){
                $admininfo=db('admin')->where('id',session('uid'))->find();
                if(!empty($admininfo)){
                    $map['jigouid']=$admininfo['jigouid'];
                }
            }
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('mediaorder')
                ->where($map)
                ->page($Nowpage, $limits)
                ->order($order)
                ->order('ordertime desc')
                ->select();
            foreach ($data as $k=>$v){
                $userinfo=db('user')->where(['acid'=>1,'id'=>$v['uid']])->find();
                $goodsinfo=db('pxcourse')->where(['acid'=>1,'id'=>$v['menuid']])->find();
                $data[$k]['goodsname']=$goodsinfo['name'];
                $data[$k]['avatar']=$userinfo['avatar'];
                $data[$k]['nickname']=$userinfo['nickname'];
                if(session('agid')!='1' && session('username')!='admin') {
                    if ($userinfo['avatar'] == '') {
                        $data[$k]['nickname'] = substr_replace($userinfo['nickname'], '******', 8, 6);
                    }
                }
                $data[$k]['ordertime']=date('Y-m-d H:i:s',$v['ordertime']);
                $data[$k]['paytime']=$v['paytime']?date('Y-m-d H:i:s',$v['paytime']):'';
                $data[$k]['is_pay']=$v['is_pay']==1?'已支付':'未支付';
                if($v['pay_type']=='wxpay'){
                    $data[$k]['pay_type']='微信支付';
                }elseif ($v['pay_type']=='alipay'){
                    $data[$k]['pay_type']='支付宝支付';
                }elseif ($v['pay_type']=='credit'){
                    $data[$k]['pay_type']='积分兑换';
                }elseif ($v['pay_type']=='yhm'){
                    $data[$k]['pay_type']='优惠码兑换';
                }elseif ($v['pay_type']=='balance'){
                    $data[$k]['pay_type']='余额支付';
                }
                if($v['apptype']=='MP-WEIXIN'){
                    $data[$k]['apptype']='微信小程序';
                }elseif ($v['apptype']=='MP-TOUTIAO'){
                    $data[$k]['apptype']='抖音|头条';
                }elseif ($v['apptype']=='MP-BAIDU'){
                    $data[$k]['apptype']='百度小程序';
                }elseif ($v['apptype']=='MP-ALIPAY'){
                    $data[$k]['apptype']='支付宝小程序';
                }elseif ($v['apptype']=='APP-PLUS'){
                    $data[$k]['apptype']='APP';
                }
                $data[$k]['avatar']=to_media_url($data[$k]['avatar']);
            }
            $total=Db::name('mediaorder')
                ->where($map)
                ->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function order_del(){
        if(request()->isAjax()) {
            $id = input('param.id');
            $res=Db::name('mediaorder')->where('id',$id)->delete();
            if($res){
                $this->result(0,'删除成功');
            }else{
                $this->result(1,'删除失败');
            }
        }
    }
    public function order_fh(){
        if(request()->isAjax()) {
            $data = input('param.');
            $id = $data['id'];

            unset($data['id']);
            $data['fh_state'] = 1;
            $res=Db::name('mediaorder')->where('id',$id)->update($data);
            if($res){
                $orderinfo=Db::name('mediaorder')
                    ->where('id',$id)
                    ->find();
                if($orderinfo['goodstype']=='mall'){
                    $goodsinfo=Db::name('goods')
                        ->where('id',$orderinfo['menuid'])
                        ->find();
                }
                if(!empty($goodsinfo)){
                    $goodsname=$goodsinfo['name'];
                }else{
                    $goodsname='商品';
                }
                $userdata=Db::name('user')
                    ->where('id',$orderinfo['uid'])
                    ->find();
                $set=$this->get_set('BASE');
                $address = Db::name('user_address')
                    ->where(['uid'=>$orderinfo['uid'],'acid'=>1,'is_mr'=>1])
                    ->find();
                if(!empty($address)){
                    $address = $address['shr_name'].' '.$address['phone'].' '.$address['area'].$address['address'];
                }else{
                    $address = '';
                }
                $msg = array(
                    'first' => array(
                        'value' => "您购买的".$goodsname."已经发货啦，请注意查收!",
                        'color' => '#ff510'
                    ),
                    'keyword1' => array(
                        'value' => $orderinfo['orderid'],
                        'color' => '#ff510'
                    ),
                    'keyword2' => array(
                        'value' => date('Y-m-d H:i',time()),
                        'color' => '#ff510'
                    ),
                    'keyword3' => array(
                        'value' => $orderinfo['wuliu'],
                        'color' => '#ff510'
                    ),
                    'keyword4' => array(
                        'value' => $orderinfo['kdnum'],
                        'color' => '#ff510'
                    ),
                    'keyword5' => array(
                        'value' => $address,
                        'color' => '#ff510'
                    ),
                    'remark' => array(
                        'value' =>  '请保持收件手机畅通！',
                        'color' => '#ff510'
                    ),
                );
                if($userdata['gzh_openid']!=''){
                    $this->sendTpl($userdata['gzh_openid'],$set['msg_fahuo'],$msg);
                }
                $this->result(0,'发货成功');
            }else{
                $this->result(1,'发货失败');
            }
        }
    }
    public function status_switch(){
        $id = input('param.id');
        $state = input('param.state');
        $data=array(
            'state'=>$state
        );
        $res=Db::name('user')->where('id',$id)->update($data);
        if($res){
            $this->result(200,'操作成功');
        }else{
            $this->result(500,'操作失败');
        }
    }
}
